var express = require('express');
var router = express.Router();

const query = require('../utils/mysql');
const jiami = require('../utils/encryption');
const jwt = require('jsonwebtoken');
const { expressjwt } = require('express-jwt');
const { gettoken, verifytoken } = require('../utils/token');
require('express-async-errors');


/*  管理员  */
router.post('/reg', async function (req, res, next) {
  try {
    const { uid, pwd } = req.body;
    const result = await query('select * from admins where username=?', [uid]);
    if (result.length > 0) {
      res.json({
        flag: false,
        msg: '用户名已存在'
      })
    } else {
      await query('insert into admins (username,password,createdAt,updatedAt) values (?,?,now(),now())', [uid, jiami(pwd)]);
      res.json({
        flag: true,
        msg: '注册成功'
      })
    }
  } catch (e) {
    next(e)
  }
});


router.post('/login', async function (req, res, next) {
  const { uid, pwd } = req.body;
  const result = await query('select * from admins where username=? and password=?', [uid, jiami(pwd)]);
  if (result.length > 0) {
    res.json({
      flag: true,
      msg: '登陆成功',
      token: gettoken({ id: result[0].id }, '1h')
    })
  } else {
    res.json({
      flag: false,
      msg: '用户名或密码错误'
    })
  }
});


router.all('*', verifytoken());

router.post('/modifyPwd', async function (req, res, next) {
  const { newPwd } = req.body;
  const { id } = req.auth;
  await query('update admins set password=? where id=?', [jiami(newPwd), id]);
  res.json({
    flag: true,
    msg: '修改成功'
  });
});


/*  新闻分类  */
router.post('/addNewsClass', async function (req, res, next) {
  const { className, classExplain } = req.body;
  const result = await query('select * from classnews where classname=?', [className]);
  if (result.length > 0) {
    res.json({
      flag: false,
      msg: '分类已存在'
    })
  } else {
    await query('insert into classnews (classname,classexplain,createdAt,updatedAt) values (?,?,now(),now())', [className, classExplain]);
    res.json({
      flag: true,
      msg: '添加分类成功'
    })
  }
});


router.post('/modifyNewsClass', async function (req, res, next) {
  const { className, classExplain, Total_id } = req.body;
  await query('update classnews set classname=?,classexplain=?,updatedAt=now() where id=?', [className, classExplain, Total_id]);
  res.json({
    flag: true,
    msg: '新闻分类修改成功'
  })
});


router.post('/deleteNewsClass', async function (req, res, next) {
  const { Total_id } = req.body;
  await query('delete from classnews where id=?', [Total_id]);
  res.json({
    flag: true,
    msg: '新闻分类删除成功'
  })
});



/*  新闻  */
router.post('/addNews', async function (req, res, next) {
  const { classID, title, content } = req.body;
  console.log(req.auth);
  const result = await query('select * from news where title=?', [title]);
  if (result.length > 0) {
    res.json({
      flag: false,
      msg: '该新闻标题已存在'
    })
  } else {
    await query('INSERT into news (title,content,authorID,classID,createdAt,updatedAt) VALUES (?,?,?,?,NOW(),NOW())', [title, content, req.auth.id, classID]);
    res.json({
      flag: true,
      msg: '新闻添加成功'
    })
  }

});


router.post('/modifyNews', async function (req, res, next) {
  const { Total_id, title, content } = req.body;
  await query('update news set title=?,content=?,updatedAt=now() where id=?', [title, content, Total_id]);
  res.json({
    flag: true,
    msg: '新闻修改成功'
  })
});


router.post('/deleteNews', async function (req, res, next) {
  const { Total_id } = req.body;
  await query('delete from news where id=?', [Total_id]);
  res.json({
    flag: true,
    msg: '新闻删除成功'
  })
});

module.exports = router;
